package freespiritsocial.io.user.ui.fragment.search

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.afollestad.materialdialogs.utils.MDUtil.updatePadding
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgForYouBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ext.setNbOnItemClickListener
import freespiritsocial.io.user.ext.showEmpty
import freespiritsocial.io.user.ui.adapter.search.ForYouPeopleAdapter
import freespiritsocial.io.user.ui.adapter.search.ForYouPlaceAdapter
import freespiritsocial.io.user.ui.adapter.search.ForYouTagsAdapter
import freespiritsocial.io.user.ui.fragment.mine.MineFrg
import freespiritsocial.io.user.viewmodel.SearchViewModel
import freespiritsocial.io.user.weight.loadCallback.EmptyCallback
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @description 搜索全部
 */
class ForYouFrg: BaseFragment<SearchViewModel,FrgForYouBinding>() {
    companion object {
        fun crate(viewModelStore: ViewModelStoreOwner) =
            ForYouFrg().apply {
                viewModelOwner = viewModelStore
                this.searchFrg = viewModelStore as SearchFrg
            }

    }

    lateinit var searchFrg: SearchFrg

    private val loadService by lazy {
        LoadSir.beginBuilder().addCallback(EmptyCallback()).build().register(mDatabind.scroll)
    }

    private val forYouPeopleAdapter by lazy(LazyThreadSafetyMode.NONE) {
        ForYouPeopleAdapter().apply {
            setOnItemClickListener { adapter,view,position ->
                val people = data[position]
                mViewModel.getUserInfo(people.userId) {
                    nav().navigateAction(R.id.action_mainFrg_to_mineFrg,bundleOf(MineFrg.KEY_IS_USER to false,MineFrg.KEY_USER to it))
                }
            }

        }
    }

    private val forYouPlaceAdapter by lazy(LazyThreadSafetyMode.NONE) {
        ForYouPlaceAdapter().apply {
            setOnItemClickListener { adapter,view,position ->
                mViewModel.getPost(data[position].invitationId)
            }
        }
    }

    private val forYouTagsAdapter by lazy(LazyThreadSafetyMode.NONE) {
        ForYouTagsAdapter().apply {
            setNbOnItemClickListener { adapter,view,position ->
                mViewModel.page.value = 1
                searchFrg.searchTag(data[position])
            }
        }
    }

    override fun layoutId(): Int = R.layout.frg_for_you

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        recyclerViewPeople.init(forYouPeopleAdapter,isScroll = false)
            .addItemDecoration(SpaceItemDecoration(0,dp2px(15f),headerCount = 1))
        forYouPeopleAdapter.setHeaderView(getHeaderView(getString(R.string.people),dp2px(20f)) {
            mViewModel.page.value = 3
        })
        recyclerViewPlaces.init(forYouPlaceAdapter,GridLayoutManager(requireContext(),3))
        forYouPlaceAdapter.setHeaderView(getHeaderView(getString(R.string.places),dp2px(5f)) {
            mViewModel.page.value = 2
        })
        recyclerViewTags.init(forYouTagsAdapter,isScroll = false)
            .addItemDecoration(SpaceItemDecoration(dp2px(20f),dp2px(15f),headerCount = 1))
        forYouTagsAdapter.setHeaderView(getHeaderView(getString(R.string.tags),dp2px(20f)) {
            mViewModel.page.value = 1
        })
        loadService.showSuccess()
    }

    private fun getHeaderView(title: String,margin: Int,viewAll: () -> Unit): View = layoutInflater.inflate(R.layout.head_search_for_you,null).apply {
        findViewById<TextView>(R.id.tv_title).also {
            it.text = title
            it.updatePadding(margin)
        }
        findViewById<TextView>(R.id.tv_view_all).also {
            it.setOnClickListener {
                viewAll()
            }
            it.updatePadding(right = margin)
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchIng.observe(this) {
            if (it) {
                mViewModel.getForYouResults()
                loadService.showSuccess()
            }
        }
        mViewModel.forYouResult.observe(this) { forYouSearchPojo ->
            forYouSearchPojo.peopleList.let {
                mDatabind.recyclerViewPeople.isVisible = it.isNullOrEmpty().not()
                it ?: return@let
                forYouPeopleAdapter.setList(it)
            }
            forYouSearchPojo.placeList.let {
                mDatabind.recyclerViewPlaces.isVisible = it.isNullOrEmpty().not()
                it ?: return@let
                forYouPlaceAdapter.setList(it)
            }
            forYouSearchPojo.tagList.let {
                mDatabind.recyclerViewTags.isVisible = it.isNullOrEmpty().not()
                it ?: return@let
                forYouTagsAdapter.setList(it)
            }
            checkIsEmpty()
        }
    }

    private fun checkIsEmpty() {
        if (mDatabind.recyclerViewPeople.isVisible.not() && mDatabind.recyclerViewPlaces.isVisible.not() && mDatabind.recyclerViewTags.isVisible.not()) {
            loadService.showEmpty()
        }
    }

    inner class Click
}